<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" @click="keywordQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表v-loading="listLoading"-->
    <el-table :data="tableDatas" v-loading="listLoading" @selection-change="handleSelectionChange" highlight-current-row
              style="width: 100%;">
      <!--多选框-->
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="70" label="序号">
      </el-table-column>
      <el-table-column prop="name" label="菜单名称" width="120">
      </el-table-column>
      <el-table-column prop="component" label="Vue组件路径" width="120">
      </el-table-column>
      <el-table-column prop="url" label="菜单路由地址" width="150">
      </el-table-column>
      <el-table-column prop="icon" label="菜单图标" width="200">
      </el-table-column>
      <el-table-column prop="index" label="菜单排序" width="200">
      </el-table-column>
      <el-table-column prop="pname" label="上级菜单" width="200">
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" :disabled="this.sels.length === 0" @click="handlePatchDel">批量删除</el-button>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
                     :page-sizes="[5, 10, 15]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                     :total="totals" style="float:right;">
      </el-pagination>
    </el-col>


    <!--添加或修改界面-->
    <el-dialog title="保存菜单" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="组件路径">
          <el-input v-model="saveForm.component" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="路由地址" prop="descs">
          <el-input v-model="saveForm.url" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单图标">
          <el-input v-model="saveForm.icon" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单排序">
          <el-input-number v-model="saveForm.index" :min="1" @change="handleChange" auto-complete="off"></el-input-number>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-cascader v-model="saveForm.parentId"
                       :options="options"
                       :props="{ checkStrictly: true,value:'id',label:'name' }"
                       clearable></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>

export default {
  data() {
    return {
      options: [],//层级
      //1.分页查询相关数据
      totals: 0,//总记录条数
      currentPage: 1,//当前页,要传递到后台的
      pageSize: 5, //每页显示多少条
      tableDatas: [], //当前页数据
      listLoading: false,

      //2.高级查询相关数据
      keyword: '',

      //3.删除相关数据

      //4.批量删除相关数据
      sels: [], //存储多选记录

      //5.添加和修改相关数据
      saveFormVisible: false,
      saveForm: {
        id:null,
        name:null,
        component:null,
        url:null,
        icon:null,
        index:null,
        parentId:null,
      },

      //6.添加和修改校验规则-表单项校验
      saveFormRules: {
        userName: [
          { required: true, message: '请输入用户名称', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' }
        ]
      },

    }
  },
  methods: {
    //1.分页查询相关方法
    //1.1.获取列表数据
    getTableData() {
      let _this = this
      let param = {
        page:_this.currentPage,
        size:_this.pageSize,
        keyword:_this.keyword
      }
      _this.$http.post("/menu/list",param).then(res=>{
        let {code,success,msg,data} = res.data
        if(success){
          _this.tableDatas = data.list
          _this.totals = data.total
        }else {
          _this.$message.error(msg);
        }
      })
    },
    //1.2.选择第几页时触发
    handleCurrentChange(curentPage) {
      this.currentPage = curentPage;
      this.getTableData();
    },
    //1.3.选择每页显示记录条数时触发
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.getTableData();
    },

    //2.高级查询相关方法
    keywordQuery() {
      this.getTableData();
    },

    //3.删除相关方法
    handleDel(row) {
      console.log(row.id)
      let _this = this
      _this.$http.delete("/menu/"+row.id).then(res=>{
        let {code,success,msg,data} = res.data
        if(success){
          //重写加载数据
          _this.$message.success("操作成功");
          _this.getTableData();
        }else {
          _this.$message.error(msg);
        }
      })
    },


    //4.批量删除相关方法
    //4.1.选项改变时触发
    handleSelectionChange(val) {
      let _this = this;
      var ids = val.map(item=>item.id)
      _this.sels = ids
    },
    //4.2.批量删除
    handlePatchDel() {
      let _this = this;
      _this.$http.post("/menu/deleteAll",_this.sels).then(res=>{
        let {code,success,msg,data} = res.data
        if(success){
          _this.$message.success("操作成功");
          //重写加载数据
          _this.getTableData();
        }else {
          _this.$message.error(msg);
        }
      })
    },

    //5.添加和修改相关方法
    //5.1.点击添加弹出对话框
    handleAdd() {
      this.saveForm = {}
      // this.saveForm.index = 1
      this.saveFormVisible = true
    },
    handleChange(value){
      console.log(value)
    },
    //5.2.点击修改弹出对话框
    handleEdit(row) {
      console.log(row)
      this.saveFormVisible = true
      //赋值
      this.saveForm = row
    },
    //5.3.提交表单数据操作数据库
    saveSubmit() {
      let _this = this
      this.$refs['saveForm'].validate((valid) => {//valid true false
        if(valid){//验证通过后才可以提交数据到后台

          var saveForm = _this.saveForm;
          if(saveForm.parentId){
            //取数组的最后一级
            saveForm.parentId = saveForm.parentId[saveForm.parentId.length-1]
          }

          _this.$http.post("/menu",_this.saveForm).then(res=>{
            let {code,success,msg,data} = res.data
            if(success){
              //关闭模态框
              this.saveFormVisible = false;

              _this.$message.success("操作成功");
              //刷新页面
              _this.getTableData()
            }else {
              _this.$message.error(msg);
            }
          })
        }
      });
    },
    getTree(){
      let _this = this;
      _this.$http.get("/menu/getTree").then(res=>{
        console.log(res.data.data)
        _this.options = res.data.data
      })
    }

  },
  mounted() {
    this.getTableData();
    this.getTree();
  }
}
</script>

